﻿namespace FiftyOne.Foundation.Mobile.Detection
{
    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Reflection;
    using System.Text;
    using System.Web;
    using System.Xml;

    internal class RequestHelper
    {
        private static readonly IPAddress[] LOCALHOSTS = new IPAddress[] { IPAddress.Parse("127.0.0.1"), IPAddress.Parse("::1") };

        internal static string GetContent(HttpRequest request, bool maximumDetail)
        {
            StringBuilder output = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(output, GetXmlSettings());
            try
            {
                writer.WriteStartElement("Device");
                writer.WriteElementString("DateSent", DateTime.UtcNow.ToString("s"));
                WriteAssembly(writer);
                if (!(request.IsLocal && IsLocalHost(IPAddress.Parse(request.UserHostAddress))))
                {
                    writer.WriteElementString("ClientIP", request.UserHostAddress);
                }
                else
                {
                    WriteHostIP(writer);
                }
                foreach (string str in request.Headers.AllKeys)
                {
                    bool flag = (str.IndexOf("cookie", 0, StringComparison.InvariantCultureIgnoreCase) >= 0) || (str == "Referer");
                    if (((maximumDetail || (str == "User-Agent")) || str.Contains("profile")) || flag)
                    {
                        if (flag)
                        {
                            WriteHeader(writer, str);
                        }
                        else
                        {
                            WriteHeaderValue(writer, str, request.Headers[str]);
                        }
                    }
                }
                writer.WriteEndElement();
                writer.Flush();
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            writer.Close();
            return output.ToString();
        }

        private static XmlWriterSettings GetXmlSettings()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = false;
            settings.ConformanceLevel = ConformanceLevel.Document;
            settings.Encoding = Encoding.UTF8;
            settings.CheckCharacters = true;
            settings.NewLineHandling = NewLineHandling.None;
            settings.CloseOutput = true;
            return settings;
        }

        private static bool IsLocalHost(IPAddress address)
        {
            foreach (IPAddress address2 in LOCALHOSTS)
            {
                if (address2.Equals(address))
                {
                    return true;
                }
            }
            return false;
        }

        private static void WriteAssembly(XmlWriter writer)
        {
            if (Assembly.GetExecutingAssembly() != null)
            {
                object[] customAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes(false);
                foreach (object obj2 in customAttributes)
                {
                    if (obj2 is AssemblyFileVersionAttribute)
                    {
                        writer.WriteElementString("Version", ((AssemblyFileVersionAttribute) obj2).Version.ToString());
                    }
                    if (obj2 is AssemblyTitleAttribute)
                    {
                        writer.WriteElementString("Product", ((AssemblyTitleAttribute) obj2).Title);
                    }
                }
            }
        }

        private static void WriteHeader(XmlWriter writer, string key)
        {
            writer.WriteStartElement("Header");
            writer.WriteAttributeString("Name", key);
            writer.WriteEndElement();
        }

        private static void WriteHeaderValue(XmlWriter writer, string key, string value)
        {
            writer.WriteStartElement("Header");
            writer.WriteAttributeString("Name", key);
            writer.WriteCData(value);
            writer.WriteEndElement();
        }

        private static void WriteHostIP(XmlWriter writer)
        {
            IPAddress[] hostAddresses = Dns.GetHostAddresses(Dns.GetHostName());
            foreach (IPAddress address in hostAddresses)
            {
                if (!(IsLocalHost(address) || (address.AddressFamily != AddressFamily.InterNetwork)))
                {
                    writer.WriteElementString("ClientIP", address.ToString());
                    return;
                }
            }
            foreach (IPAddress address in hostAddresses)
            {
                if (!IsLocalHost(address))
                {
                    writer.WriteElementString("ClientIP", address.ToString());
                    break;
                }
            }
        }
    }
}

